package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.common.PageResult;
import com.heima.common.Result;
import com.heima.common.ResultQuery;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.dto.AddRegionDto;
import com.heima.pojo.entity.Region;
import com.heima.service.RegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.geom.Area;
import java.util.List;

@Service
@Slf4j
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {
    @Autowired
    private RegionMapper regionMapper;

    /**
     * 地区查询
     *
     * @param query
     * @return
     */
    @Override
    public PageResult<Region> searchQuly(ResultQuery query) {
        log.info("地区查询,{}", query);

        PageHelper.startPage(query.getPageIndex(), query.getPageSize());

        List<Region> list = regionMapper.searchQuery(query);


        PageInfo<Region> pageInfo = new PageInfo<>(list);

        PageResult<Region> pageResult = PageResult.<Region>builder()
                .pageIndex(query.getPageIndex()) // 当前页码
                .pageSize(query.getPageSize()) // 每页记录数
                .totalCount(pageInfo.getTotal()) // 总记录数
                .totalPage(pageInfo.getPages()) // 总页数
                .currentPageRecords(pageInfo.getList()) // 当前页记录
                .build();
        return pageResult;
    }

    /**
     * 新增区域
     *
     * @param dto
     * @return
     */
    @Override
    public Result<Boolean> add(AddRegionDto dto) {
        log.info("新增区域,{}", dto);
        Region region = new Region();
        region.setName(dto.getRegionName());
        region.setRemark(dto.getRemark());
        regionMapper.insert(region);
        return Result.success(true);
    }


    /**
     * 删除区域
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteRegion(Long id) {
        log.info("删除区域: {}", id);
        int result = regionMapper.deletesById(id);
        if (result > 0) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败，未找到该记录");
        }
    }
}